;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.ThreeOffset", [])
        .controller("ThreeOffsetCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService) {
            $scope.activeFlag = [true, false, false, false, false];
            $scope.datePickerFlag = [false, false];
            $scope.submitted = false;
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.deliveryPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.orderCondition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];
            $scope.threeOutList = [];
            $scope.detailInfo = {};
            $scope.steps = {
                curStep: 0,
                list: [{
                    name: '选择三包出库单'
                }, {
                    name: '生成三包冲销单'
                }]
            };
            $scope.stateList = [{
                    id: 0,
                    name: "未入库"
                },
                {
                    id: 1,
                    name: "部分入库"
                },
                {
                    id: 2,
                    name: "已入库"
                }
            ];
            $scope.auditState = [{
                    id: 0,
                    name: "未审核"
                },
                {
                    id: 1,
                    name: "审核通过"
                },
                {
                    id: 2,
                    name: "审核拒绝"
                }
            ];
            $scope.sapState = [{
                    id: 1,
                    name: "是"
                },
                {
                    id: 0,
                    name: "否"
                }
            ];
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/threeReverse/export"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/threeReverse/exportAll")
            };
            $scope.regionAllotCodeInList = [];
            $scope.regionAllotCodeOutList = [];
            $scope.tableConfig = {
                fixedWidth: 280,
                scrollWidth: 1400,
                tableCol: [{
                        type: "",
                        name: "冲销入库单号",
                        dataField: "reverseOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                        multiClick: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "三包出库单号",
                        dataField: "businessOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    /* -------------新增转入、转出单号--------------- */
                    {
                        type: "",
                        name: "转入单号",
                        dataField: "regionAllotCodeIn",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewAllocationInDetail",
                        multiClick: true,
                        isIn: true,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "转出单号",
                        dataField: "regionAllotCodeOut",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewAllocationOutDetail",
                        multiClick: true,
                        isOut: true,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "转入状态",
                        dataField: "turnInFlagShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "转出状态",
                        dataField: "turnOutFlagShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    /* ---------------------------------------- */
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "agentName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "仓库编码",
                        dataField: "warehouseCode",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "入库状态",
                        dataField: "stateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "审核状态",
                        dataField: "auditStateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "三包出库单是否过账",
                        dataField: "isPostingShow", // 0：否；1：是
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "SAP是否冲销成功",
                        dataField: "isSapReverseShow",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单时间",
                        dataField: "createDateShows",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    /* -------------wangcan:需求变更：列表中增加服务信息单号字段-------------- */
                    {
                        type: "",
                        name: "服务信息单号",
                        dataField: "serviceInformationMember",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    /* -------------------------------------------------------------- */
                    {
                        type: "",
                        name: "备注",
                        dataField: "note",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 280,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.threeOutTbConf = {
                scrollWidth: 950,
                tableCol: [{
                        type: "",
                        name: "三包出库单号",
                        dataField: "guaranteesOutNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "sap预留单号",
                        dataField: "sapOrder",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件凭证",
                        dataField: "voucherNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "申请人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库状态",
                        dataField: "outState",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.materielTbConf = {
                scrollWidth: 830,
                tableCol: [{
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "计划出库数量",
                        width: 100,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际出库数量",
                        width: 100,
                        dataField: "amount"
                    },
                    {
                        name: "状态",
                        width: 100,
                        dataField: "statusShow"
                    }
                ]
            };
            $scope.detailTbConf = {
                scrollWidth: 1070,
                tableCol: [{
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "申请发货数量",
                        width: 110,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际发货数量",
                        width: 110,
                        dataField: "realQty"
                    },
                    {
                        name: "计划入库数量",
                        width: 110,
                        dataField: "amount"
                    },
                    {
                        name: "实际入库数量",
                        width: 110,
                        dataField: "realAmount"
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "stateShow"
                    }
                ]
            };
            $scope.auditTbConf = {
                scrollWidth: 1070,
                tableCol: [{
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "申请发货数量",
                        width: 110,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际发货数量",
                        width: 110,
                        dataField: "realQty"
                    },
                    {
                        name: "计划入库数量",
                        width: 110,
                        dataField: "amount"
                    },
                    {
                        name: "实际入库数量",
                        width: 110,
                        dataField: "realAmount"
                    },
                    {
                        name: "状态",
                        width: 100,
                        dataField: "stateShow"
                    }
                ]
            };
            $scope.inTbConf = {
                scrollWidth: 1090,
                tableCol: [{
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo"
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialName"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "申请发货数量",
                        width: 110,
                        dataField: "deliveryQty"
                    },
                    {
                        name: "实际发货数量",
                        width: 110,
                        dataField: "realQty"
                    },
                    {
                        name: "计划入库数量",
                        width: 110,
                        dataField: "amount"
                    },
                    {
                        name: "实际入库数量",
                        width: 110,
                        dataField: "realAmount"
                    },
                    {
                        name: "储位",
                        width: 150,
                        dataField: "locationCode",
                        input: true,
                        inputType: "func"
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "stateShow",
                        input: false,
                        inputType: "",


                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    }
                ]
            };
            $scope.tabFlag = {};
            var sortParam = "";
            var sortFiled = {};

            var btnPermissionCode = {
                threeOffsetAdd: window.ActionCode.threeOffsetAdd,
                threeOffsetEdit: window.ActionCode.threeOffsetEdit,
                threeOffsetDel: window.ActionCode.threeOffsetDel,
                threeOffsetAudit: window.ActionCode.threeOffsetAudit,
                threeOffsetExport: window.ActionCode.threeOffsetExport,
                threeOffsetIn: window.ActionCode.threeOffsetIn,
                threeOffsetPrint: window.ActionCode.threeOffsetPrint,
                threeOffsetPosting: window.ActionCode.threeOffsetPosting,
                threeOffsetTranIn: window.ActionCode.threeOffsetTranIn,
                threeOffsetTranOut: window.ActionCode.threeOffsetTranOut,
            };

            $scope.btnPermission = {
                threeOffsetAdd: true,
                threeOffsetEdit: true,
                threeOffsetDel: true,
                threeOffsetAudit: true,
                threeOffsetExport: true,
                threeOffsetIn: true,
                threeOffsetPrint: true,
                threeOffsetPosting: true,
                threeOffsetTranIn: true,
                threeOffsetTranOut: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.submitted = false;
                $scope.detailInfo = {};
                $scope.toolbarConfig.showMateriel = false;
                $scope.steps.curStep = 0;
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    allChecked: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/threeReverse/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item, index) {
                        item.isChecked = false;
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                        item.turnInFlagShow = $filter("ProgressStatus")(item.turnInFlag, "transforIn");
                        item.turnOutFlagShow = $filter("ProgressStatus")(item.turnOutFlag, "transforOut");
                        item.isPostingShow = $filter("TrueOrFalse")(item.isPosting);
                        item.isSapReverseShow = $filter("TrueOrFalse")(item.isSapReverse);
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                        item.auditStateShow = $filter("ProgressStatus")(item.auditState, "audit");
                        if (!!item.regionAllotCodeIn) {
                            item.regionAllotCodeInList = item.regionAllotCodeIn.split(',');
                        } else {
                            $scope.regionAllotCodeInList[index] = [];
                            item.regionAllotCodeInList = [];
                        }
                        if (!!item.regionAllotCodeOut) {
                            item.regionAllotCodeOutList = item.regionAllotCodeOut.split(',');
                        } else {
                            item.regionAllotCodeOutList = [];
                        }
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;

                });
            }

            $scope.toSort = function (config, index) {
                // return;
                if (!config[index].toSort) {
                    return;
                }
                var sortType = ybWidget.ToSort(config, index);
                sortFiled = config[index];
                sortParam = sortFiled.sortField + " " + sortType;
                getDataList(1);
            };

            function clearSortParam() {
                sortParam = "";
                if (angular.isDefined(sortFiled.sortType)) {
                    sortFiled.sortType = ""
                }
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    case "viewAllocationInDetail": // 点击转入，跳转区域调拨单页面
                        viewAllocationInDetail(item);
                        break;
                    case "viewAllocationOutDetail": // 点击转出，跳转区域调拨单页面
                        viewAllocationOutDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {
                    seller: true
                };
            };

            function geBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
                geBillIds();
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                geBillIds();
            };

            $scope.printBill = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: "SaleOffset"
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            $scope.addApply = function () {
                getDeliveryNotice(1);
                $scope.changeTab(1);
            };

            function getDeliveryNotice(curPage) {
                var service = "device";
                var reqUrl = "/threeGuaranteesOut/list";
                var param = {};
                param.pageSize = $scope.deliveryPageInfo.pageSize;
                if (!!curPage) {
                    $scope.deliveryPageInfo.curPage = curPage;
                }
                param.pageNum = $scope.deliveryPageInfo.curPage;
                param.filters = {};
                param.filters.reverseState = 1;
                param.filters.guaranteesOutNo = $scope.orderCondition.guaranteesOutNo;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.deliveryPageInfo.totalRows = resp.value.total;
                    $scope.deliveryPageInfo.totalPages = resp.value.pages;
                    $scope.threeOutList = resp.value.list;
                    angular.forEach($scope.threeOutList, function (item) {
                        item.isChecked = false;
                        item.outState = $filter("ProgressStatus")(item.outLocationStatus, "outDepot");
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                    });
                });
            }

            $scope.selThreeOut = function (item) {
                $scope.steps.curStep = 1;
                var materialList = [];
                ybHttp.Get('device', '/threeGuaranteesOut/item', {
                    id: item.id
                }).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.detailInfo.headerInfo = resp.value.headerInfo;
                    angular.forEach(resp.value.detailList, function (item) {
                        var materialItem = {};
                        materialItem.materialNo = item.materialsNo;
                        materialItem.materialName = item.materialsDesc;
                        materialItem.unit = item.unit;
                        materialItem.deliveryQty = item.deliveryQty;
                        materialItem.realQty = item.realOutQty;
                        materialItem.amount = item.realOutQty;
                        materialItem.statusShow = $filter("ProgressStatus")(item.outLocationStatus, "outDepot");
                        materialList.push(materialItem);
                    });
                    $scope.detailInfo.detailList = materialList;

                });
            };

            $scope.orderSearch = function () {
                getDeliveryNotice();
            };

            $scope.delData = function (delType, billId) {
                var service = "device";
                var reqUrl = "/threeReverse/remove";
                var billIds = [];

                if (delType === "single") {
                    billIds.push(billId);
                }

                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            billIds.push($scope.dataList[i].id);
                        }
                    }
                }

                if (billIds.length === 0) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, billIds).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            /* 转出操作 */
            $scope.transforOut = function (item) {
                /* get接口获取需要带到区域调拨单上的数据 */
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    } else {
                        var detailVOList = [];
                        angular.forEach(data.detailList, function (item) {
                            detailVOList.push({
                                materialsNo: item.materialNo,
                                materialsDes: item.materialName,
                                unit: item.unit,
                                enableNum: item.enableNum,
                                amount: item.realAmount,
                            })
                        });
                        EventBus.Publish('go2otherMenuPage', {
                            name: "app.stock.allocation",
                            params: {
                                canKaoObj: {
                                    threeReverseId: item.id,
                                    warehouseCode: item.warehouseCode,
                                    inArea: item.inArea,
                                    unit: item.unit,
                                    enableNum: item.enableNum,
                                    realAmount: item.realAmount,
                                    threeReverseType: "1",
                                    detailVOList: detailVOList,
                                }
                            }
                        });
                    }
                });
            };

            /* 转入操作 */
            $scope.transforIn = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    } else {
                        var detailVOList = [];
                        angular.forEach(data.detailList, function (item) {
                            detailVOList.push({
                                materialsNo: item.materialNo,
                                materialsDes: item.materialName,
                                unit: item.unit,
                                enableNum: item.enableNum,
                                amount: item.realAmount,
                            })
                        });
                        EventBus.Publish('go2otherMenuPage', {
                            name: "app.stock.allocation",
                            params: {
                                canKaoObj: {
                                    threeReverseId: item.id,
                                    warehouseCode: item.warehouseCode,
                                    inArea: item.inArea,
                                    unit: item.unit,
                                    enableNum: item.enableNum,
                                    realAmount: item.realAmount,
                                    threeReverseType: "2",
                                    detailVOList: detailVOList,
                                }
                            }
                        });
                    }
                });
            };

            function getDetail(item) {
                var service = "device";
                var reqUrl = "/threeReverse/item";
                var param = {
                    id: item.id
                };
                return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }


            function viewDetail(item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    angular.forEach(data.detailList, function (item) {
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    });
                    $scope.detailInfo = data;
                    $scope.detailInfo.headerInfo.isReturnFactory = $filter("TrueOrFalse")(data.headerInfo.isReturnFactory, "normal");
                    $scope.changeTab(2);
                });
            }


            /* 点击转入转出单号：跳转区域调拨单详情 */
            function viewAllocationInDetail(item) {
                $state.go("app.stock.allocation", {
                    canKaoObj: {
                        regionAllotCodeIn: item, // 转入单号
                        regionAllotCodeType: "1", // 转入/转出类型
                    }
                })
            }

            function viewAllocationOutDetail(item) {
                $state.go("app.stock.allocation", {
                    canKaoObj: {
                        regionAllotCodeOut: item, // 转出单号
                        regionAllotCodeType: "2", // 转入/转出类型
                    }
                })
            }

            $scope.auditData = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    angular.forEach(data.detailList, function (item) {
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    });
                    $scope.detailInfo = data;
                    $scope.changeTab(3);
                });
            };

            $scope.saveData = function () {
                var service = "device";
                var reqUrl = "/threeReverse/add";
                var param = {
                    headerInfo: {},
                    detailList: []
                };
                param.headerInfo.businessOrder = $scope.detailInfo.headerInfo.guaranteesOutNo;
                param.headerInfo.agentId = $scope.detailInfo.headerInfo.agentId;
                param.headerInfo.agentName = $scope.detailInfo.headerInfo.agentName;
                param.headerInfo.isPosting = $scope.detailInfo.headerInfo.isPosting;
                param.headerInfo.note = $scope.detailInfo.headerInfo.note;
                param.headerInfo.warehouseCode = $scope.detailInfo.headerInfo.outLocation;
                param.headerInfo.inArea = $scope.detailInfo.headerInfo.outArea;
                param.headerInfo.sapInArea = $scope.detailInfo.headerInfo.sapOutArea;
                param.headerInfo.deptId = $scope.curUser.userId;
                param.headerInfo.deptName = $scope.curUser.deptName;
                /* 服务信息单号、是否返厂 */
                param.headerInfo.serviceInformationMember = $scope.detailInfo.headerInfo.serviceInformationMember;
                param.headerInfo.isReturnFactory = $scope.detailInfo.headerInfo.isReturnFactory;
                param.detailList = angular.copy($scope.detailInfo.detailList);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList(1);
                    $scope.changeTab(0);
                });
            };

            $scope.auditPurchase = function (id, state, message) {
                var service = "device";
                var reqUrl = "/threeReverse/audit";
                if (state === 2) {
                    if (!message) {
                        ybWidget.ShowMessage('请输入审核意见');
                        $scope.submitted = true;
                        return;
                    }
                }
                var param = {
                    id: id,
                    auditState: state,
                    auditNote: message
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList(0);
                });
            };

            $scope.prevStep = function () {
                $scope.steps.curStep = 0;
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.inbound = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.detailInfo = data;
                    angular.forEach($scope.detailInfo.detailList, function (item) {
                        item.stateShow = $filter("ProgressStatus")(item.state, "inDepot");
                    });
                    $scope.changeTab(4);
                });
            };

            $scope.selDepot = function (item) {
                var param = {
                    warehouseCode: $scope.detailInfo.headerInfo.warehouseCode
                };
                ybWidget.DepotSelector(param).then(function (data) {
                    item.warehouseCode = data.warehouseCode;
                    item.areaCode = data.areaCode;
                    item.locationCode = data.locationCode;
                })
            };

            $scope.clearDepot = function (item) {
                item.warehouseCode = "";
                item.areaCode = "";
                item.locationCode = "";
            };

            $scope.inboundSubmit = function () {
                var service = "device";
                var reqUrl = "/warehouseInboundRecord/receive";
                var param = {
                    headerInfo: {},
                    detailList: []
                };
                param.headerInfo.businessOrder = $scope.detailInfo.headerInfo.reverseOrder;
                angular.forEach($scope.detailInfo.detailList, function (item) {
                    var listRow = {};
                    listRow.materialNo = item.materialNo;
                    listRow.warehouseCode = item.warehouseCode;
                    listRow.areaCode = item.areaCode;
                    listRow.locationCode = item.locationCode;
                    param.detailList.push(listRow);
                });
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };

            $scope.posting = function (item) {
                var service = "device";
                var reqUrl = "/threeReverse/sapPost";
                var param = {};
                param.id = item.id;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };

            $scope.printData = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: "ThreeOffset"
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);